package com.sale.plugins;

import com.apache.api.vo.ParamsVo;
import com.apache.database.constant.SpringContextLoader;
import com.apache.database.constant.SystemTools;
import com.apache.ius.plugin.CustomMethodPlugin;
import com.apache.match.manager.DealMatchingManager;
import com.apache.tools.StrUtil;
import org.apache.log4j.Logger;

import java.util.HashMap;
import java.util.Map;

/**
 * 初始化撮合队列
 */
public class InitDealMatchingPluginImpl implements CustomMethodPlugin {

    private Logger log = Logger.getLogger(getClass());

    private DealMatchingManager dealMatchingManager;

    public Object beforeInvoke(Map<String, Object> param) {
        //		initMatchQueue();
        return null;
    }

    public Object doInvoke(Map<String, Object> param) {
        // TODO Auto-generated method stub
        return null;
    }

    public Object afterInvoke(Map<String, Object> param) {
        //		GoEasy goEasy = new GoEasy("fcdd060e-c3ee-4003-bdd7-d68785a77d19");
        //		goEasy.publish("my_channel", "InitDealMatchingPluginImpl发送的消息!");
        Map<String, Object> resultMap = initMatchQueue(param);
        return resultMap;
    }

    /**
     * 规则校验入口（专门给系统公式规则提供的）
     *
     * @param paramsVo
     */
    public void run(ParamsVo paramsVo) {
        beforeInvoke(paramsVo.getParams());
    }

    private Map<String, Object> initMatchQueue(Map<String, Object> param) {
        Map<String, Object> resultMap = new HashMap<String, Object>();
        resultMap.put("flag", "F");
        String type = param.get("type").toString();
        log.info("初始化撮合队列："+param);
        if (StrUtil.isNotNull(type)) {
            String initFlag = String.valueOf(
                    SystemTools.getInstance().getCache("").getCacheObjectByKey("InitDealMatching"));
            if ("view".equals(type)) {
                if ("T".equalsIgnoreCase(initFlag)) {
                    resultMap.put("status", "T");
                    resultMap.put("msg", "启用中");
                } else {
                    resultMap.put("status", "F");
                    resultMap.put("msg", "停用中");
                }
                log.info("查询撮合服务状态：" + resultMap);
            } else {
                log.info("重启撮合服务...");
                if (null == dealMatchingManager) {
                    dealMatchingManager = ((DealMatchingManager) SpringContextLoader
                            .getBean("dealMatchingManager"));
                }
                if (!"T".equalsIgnoreCase(initFlag)) {
                    dealMatchingManager.loadData(new ParamsVo());
                    SystemTools.getInstance().getCache("")
                            .createCacheObject("InitDealMatching", "T");
                } else {
                    //dealMatchingManager.clearData(new ParamsVo());
                    SystemTools.getInstance().getCache("").removeCacheObject("MATCH_PRODUCT_FLAG");
                    SystemTools.getInstance().getCache("").removeCacheObject("MATCH_INDEX");
                    //清理正常完成后，自动转为暂停服务状态
                    SystemTools.getInstance().getCache("")
                            .createCacheObject("InitDealMatching", "F");
                    dealMatchingManager.loadData(new ParamsVo());
                }
            }
            resultMap.put("flag", "T");
        } else {
            resultMap.put("msg", "缺失请求参数type");
        }
        return resultMap;
    }
}
